package com.service.system.impl;

import com.dao.system.UserDao;
import com.domain.system.Module;
import com.domain.system.Role;
import com.domain.system.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.system.UserService;
import common.utils.Encrypt;
import common.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public PageInfo findAll(String companyId, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<User> list = userDao.findAll(companyId);
        return new PageInfo(list);
    }

    @Override
    public Integer save(User user) {
        user.setId(UUID.randomUUID().toString());
        user.setPassword(Encrypt.md5(user.getPassword(), user.getEmail()));
        return userDao.save(user);
    }

    @Override
    public Integer update(User user) {
        return userDao.update(user);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public Integer delete(String id) {
        return userDao.delete(id);
    }

    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

    @Override
    public Integer updateRole(String userId, String oldRoleIds) {
        Integer result = userDao.deleteRole(userId);
        String[] roleIds = oldRoleIds.split(",");
        for (String roleId : roleIds) {
            userDao.insertRole(userId, roleId);
        }
        return null;
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

}
